package com.ci.test;

import java.io.BufferedReader;
import java.io.File;
import java.io.IOException;
import java.io.InputStream;
import java.io.InputStreamReader;
import java.text.SimpleDateFormat;
import java.util.Date;

public class LogApi {

	public static void main(String[] args) throws InterruptedException {
	LogApi logapi=new LogApi();
	logapi.execCmd("adb logcat -c");
	String fileName=logapi.getCurrentSysTime()+".txt";
    Process p=logapi.execCmd("adb logcat -v time  > "+fileName);
    Thread.sleep(10000);
	//logapi.stopLogcatOut(fileName);
	System.out.println("----------------done------------");
	
	}
	

	
	
	/**
	 * 需求:中止一个process进程
	 * @param p
	 */
	public int stopProcess(Process p) {
		return p.exitValue();
	}
	/**
	 * 需求：中止一个logcat输出到某个文件<\br>
	 * 思路：pull后删除文件
	 */
	public void stopLogcatOut(String logFile) {
		File file=new File("log");
		if (!file.exists()) {
			file.mkdir();
		}
		Process process=execCmd("adb shell pull  "+logFile+" log");
		try {
			process.waitFor();
		} catch (InterruptedException e) {
			e.printStackTrace();
		}
		execCmd("adb shell rm "+logFile);		
	}
	/**
	 * 需求：运行系统命令,不获取信息
	 * 
	 * @param cmd
	 */
	public Process execCmd(String cmd) {
		System.out.println("----execCmd:  " + cmd);
		Process p = null ;
		try {
			p = Runtime.getRuntime().exec(cmd);
		} catch (IOException e) {
			e.printStackTrace();
		}
		return p;
	}
	/**
	 * 需求：需求：获取当前系统的时间，到毫秒（范翰卿）
	 * 
	 * @return
	 */
	public String getCurrentSysTime() {
		SimpleDateFormat formattime1 = new SimpleDateFormat(
				"yyyyMMdd-HHmmss");
		long ctime = System.currentTimeMillis();
		String currenttime = formattime1.format(new Date(ctime));
		System.out.println(currenttime);
		return currenttime;
	}
	
     
}
